package wicketcool;

import wicketcool.common.configuration.WicketCoolConfiguration;
import wicketcool.common.testutils.SystemOutMock;
import wicketcool.model.WCLClass;
import wicketcool.model.WCLInterface;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.mockito.Mockito.when;

/**
 * User: rabbit
 * Date: Sep 6, 2009
 */
public class DomainTestBase {

    protected final SystemOutMock systemOutMock = new SystemOutMock();

    protected final String PROJECT_NAME = "projectName";
    protected final String PACKAGE_NAME = "packName";
    protected final String DEFAULT_DOMAIN_ENTITIES_PATH = PROJECT_NAME + ".domain" + File.separator + "src" + File.separator + "main"
            + File.separator + "java" + File.separator + PACKAGE_NAME + File.separator + "domain" + File.separator + "entities";
    protected final String DEFAULT_DOMAIN_DAOS_PATH = PROJECT_NAME + ".domain" + File.separator + "src" + File.separator + "main"
            + File.separator + "java" + File.separator + PACKAGE_NAME + File.separator + "domain" + File.separator + "dao";
    protected final String DEFAULT_SERVICE_SOURCE_CLASSES_PATH = PROJECT_NAME + ".service" + File.separator + "src" + File.separator + "main"
            + File.separator + "java" + File.separator + PACKAGE_NAME + File.separator + "service";

    protected WicketCoolConfiguration createConfiguration() {
        WicketCoolConfiguration configuration = new WicketCoolConfiguration(new HashMap<String, String>());
        configuration.setProjectName(PROJECT_NAME);
        configuration.setPackageName(PACKAGE_NAME);
        configuration.createDefaultsIfNeeded();
        return configuration;
    }

    protected Object[] createdClass(JavaClassGenerator generator) {

        Object[] created = new Object[4];
        created[0] = createdEntity(generator);
        created[1] = createdDao(generator);
        created[2] = createdDaoInterface(generator);
        created[3] = createdServiceInterface(generator);

        return created;
    }

    protected List<String> argumentsAsList(String... arguments) {
        List<String> argumentsList = new ArrayList<String>();

        for (String argument : arguments) {
            argumentsList.add(argument);
        }
        return argumentsList;

    }

    protected WCLClass createdDao(JavaClassGenerator generator) {
        WCLClass dao = new WCLClass("UserDAOImpl", PACKAGE_NAME + ".domain.dao");
        when(generator.initClass("UserDAOImpl", PACKAGE_NAME + ".domain.dao")).thenReturn(dao);
        return dao;
    }

    protected WCLClass createdEntity(JavaClassGenerator generator) {
        WCLClass entity = new WCLClass("User", PACKAGE_NAME + ".domain.entities");
        when(generator.initClass("User", PACKAGE_NAME + ".domain.entities")).thenReturn(entity);
        return entity;
    }

    protected WCLInterface createdDaoInterface(JavaClassGenerator generator) {
        WCLInterface dao = new WCLInterface("UserDAO", PACKAGE_NAME + ".domain.dao");
        when(generator.initInterface("UserDAO", PACKAGE_NAME + ".domain.dao")).thenReturn(dao);
        return dao;
    }

    protected WCLInterface createdServiceInterface(JavaClassGenerator generator) {
        WCLInterface service = new WCLInterface("UserService", PACKAGE_NAME + ".service");
        when(generator.initInterface("UserService", PACKAGE_NAME + ".service")).thenReturn(service);
        return service;
    }
}
